home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 1
/
Nebula One.iso
/
Utilities
/
Converters
/
Convert_FONT
/
Source
/
shared.subproj
/
RCS
/
TextFile.m,v
< prev
Wrap
Text File
|
1995-06-12
|
13KB
|
402 lines
head 1.3;
branch ;
access ;
symbols beta10:1.2;
locks death:1.3;
comment @@;
1.3
date 93.04.04.23.45.30; author death; state Exp;
branches ;
next 1.2;
1.2
date 93.01.10.15.08.57; author death; state Exp;
branches ;
next 1.1;
1.1
date 92.07.26.13.58.54; author death; state Exp;
branches ;
next ;
desc
@Initial revision of an object for dealing with text files...
@
1.3
log
@Sun Apr 4 23:45:30 PDT 1993
@
text
@#import <streams/streams.h>
#import <stdio.h>
#import <string.h> // for strlen
#import "TextFile.h"
// We are assuming our superclass has already imported this. If not, you'll get an
// error below, indicating that this wont work (see LookAtNextCharacter
// #import <streams/streams.h>
@@implementation TextFile
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: GetHexByte
// Parameters: none
// Returns: The byte corresponding to the hex values in the file
// Stores: error info
// The byte corresponding to the hex values in the file
// Description:
// This reads in a pair of hex difits, and returns their integer form (i.e reading
// '41' will be returned as the integer 65.
// *WARNING*:
// This assumes it is working as a subclass of a File object that is going to
// have a NXstream open.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (Byte) GetHexByte
{
Integer theInt = 0;
Integer num;
[self ResetResults];
if (FileIsOpen == NO)
[self StoreErrorCode: ERR_FILENOTOPEN AndText: "Unable to read from a closed file"];
else
{
num = NXScanf(TheFile, "%2x", &theInt);
if (num != 1)
[self StoreErrorCode: ERR_BADREAD AndText: "Unable to read a hex value"];
else
[self StoreErrorCode: ERR_OK AndText: "Got the byte just fine"];
}
[self StoreInteger: theInt];
return theInt;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: GetNumber
// Parameters: none
// Returns: The number read from the text file
// Stores: error info
// The number read from the text file
// Description:
// This reads in a series of characters, and converts them to a signed integer
// value.
// *WARNING*:
// This assumes it is working as a subclass of a File object that is going to
// have a NXstream open.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (Integer) GetNumber
{
Integer theInt = 0;
Integer num;
[self ResetResults];
if (FileIsOpen == NO)
[self StoreErrorCode: ERR_FILENOTOPEN AndText: "Unable to read from a closed file"];
else
{
num = NXScanf(TheFile, "%d", &theInt);
if (num != 1)
[self StoreErrorCode: ERR_BADREAD AndText: "Unable to read a hex value"];
else
[self StoreErrorCode: ERR_OK AndText: "Got the byte just fine"];
}
[self StoreInteger: theInt];
return theInt;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: GetCharacter:
// Parameters: none
// Returns: The character found
// Stores: error info
// The character found
// Description:
// This merely reads a character in from the file and returns it, unless an error
// occurrs. Note that if the file is open, our error results are those which
// Read:BytesInto: generates.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (Character) GetCharacter
{
Character theBuffer = NullCharacter;
[self ResetResults];
if (FileIsOpen == NO)
[self StoreErrorCode: ERR_FILENOTOPEN AndText: "Unable to read from a closed file"];
else
{
[self Read: 1 BytesInto: &theBuffer];
if ([self GetErrorCode] != ERR_OK)
{
[self StoreErrorCode: ERR_BADREAD AndText: "Read error. let's call it eof"];
FileLocation = fileAtEOF;
}
else
[self StoreErrorCode: ERR_OK AndText: "OK"];
}
[self StoreCharacter: theBuffer];
return theBuffer;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: LookAtNextCharacter
// Parameters: none
// Returns: The character found
// Stores: error info
// The character found
// Description:
// This locates the next character waiting to be read, and returns it without
// moving past it.
// *WARNING*:
// This assumes it is working as a subclass of a File object that is going to
// have a NXstream open that can do ungetting!!!!!!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (Character) LookAtNextCharacter
{
Character theCharacter = NullCharacter;
[self ResetResults];
if (FileIsOpen == NO)
[self StoreErrorCode: ERR_FILENOTOPEN AndText: "Unable to peek at a closed file"];
else
{
if (NXAtEOS(TheFile) == YES)
{
[self StoreErrorCode: ERR_BADREAD AndText: "Read error. let's call it eof"];
FileLocation = fileAtEOF;
}
else
{
//
// NeXT doesn't document what error codes these generate, very well..
//
theCharacter = NXGetc(TheFile);
NXUngetc(TheFile);
[self StoreErrorCode: ERR_OK AndText: "Read the character OK"];
}
}
[self StoreCharacter: theCharacter];
return theCharacter;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: UnGetCharacter
// Parameters: none
// Returns: none
// Stores: error info
// Description:
// Thgis simply ungets whatever the last character got was.
// (due to NeXT limitiations, you can't call this twice in a row to unget the last 2)
// *WARNING*:
// This assumes it is working as a subclass of a File object that is going to
// have a NXstream open that can do ungetting!!!!!!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- UnGetCharacter
{
[self ResetResults];
if (FileIsOpen == NO)
[self StoreErrorCode: ERR_FILENOTOPEN AndText: "Unable to peek at a closed file"];
else
{
if (NXAtEOS(TheFile) == YES)
{
[self StoreErrorCode: ERR_BADREAD AndText: "Read error. let's call it eof"];
FileLocation = fileAtEOF;
}
else
{
//
// NeXT doesn't document what error codes these generate, very well..
//
NXUngetc(TheFile);
[self StoreErrorCode: ERR_OK AndText: "Ungot it ok"];
}
}
return self;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: WriteText:
// Parameters: the line of text we are to write
// Returns: self
// Description:
// Writes the content6s of theLine out litterally, with no additional fiddling with.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- WriteText: (CString) theLine
{
[self Write: strlen(theLine) BytesFrom: (ByteString) theLine];
return self;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: WriteTextLine:
// Parameters: the line of text we are to write
// Returns: self
// Description:
// This writes out the speciied text, and follows it with a newline.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- WriteTextLine: (CString) theLine
{
[self Write: strlen(theLine) BytesFrom: (ByteString) theLine];
[self Write: 1 BytesFrom: (ByteString) "\n"];
return self;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: WriteTextUsing:WithFormat:
// Parameters: A buffer provided by the caller, a format, and N arguments to be written
// Returns: self
// Stores: nothing (bug)
// Description:
// This writes out a text string given variable arguments
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- WriteTextUsing: (CString) buffer WithFormat: (CString) format, ...;
{
va_list parameter_list;
va_start(parameter_list, format);
vsprintf(buffer, format, parameter_list); // doc implies this does a va_end
[self Write: strlen(buffer) BytesFrom: (ByteString) buffer];
return self;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: AppendFrom:
// Parameters: Another file object (opened)
// Returns: self
// Stores: nothing (bug)
// Description:
// This is a crude little routine that simply takes a file, assumes it is open and in
// place, reads the whole file into memory, and then writs it out into itself.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- AppendFrom: sourceFile
{
Integer size;
ByteString buffer;
size = [sourceFile FileSize];
buffer = NewByteString(size);
[sourceFile MoveTo: 0];
[sourceFile Read: size BytesInto: buffer];
[self Write: size BytesFrom: buffer];
FreeByteString(buffer);
return self;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: WriteInteger:
// Parameters:
// An integer
// Returns: self
// Stores: none explicitly (subcalls may)
// Description:
// This simply writes out an integer with a trailing space. ooh ahh.
// Bugs:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- WriteInteger: (Integer) theInt
{
CString temp = NewCString(15);
[self WriteTextUsing: temp WithFormat: "%d ", theInt];
FreeCString(temp);
return self;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: WritePositiveInteger:
// Parameters:
// An integer
// Returns: self
// Stores: none explicitly (subcalls may)
// Description:
// This simply writes out a positive integer with a trailing space. yawn.
// Bugs:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- WritePositiveInteger: (PositiveInteger) theInt
{
CString temp = NewCString(15);
[self WriteTextUsing: temp WithFormat: "%u ", theInt];
FreeCString(temp);
return self;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method: WriteReal:
// Parameters: A real number
// Returns: self
// Stores: none explicitly (subcalls may)
// Description:
// This simply writes out a Real number with a trailing space..
// Bugs:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- WriteReal: (Real) theReal
{
CString temp = NewCString(31);
[self WriteTextUsing: temp WithFormat: "%.15f ", theReal];
FreeCString(temp);
return self;
}
@@end@
1.2
log
@Sun Jan 10 15:08:57 PST 1993
@
text
@@
1.1
log
@Initial revision
@
text
@d8 1
a8 1
// error below, indicating that this wont work (see LookAtNextCharacter
d140 14
a153 6
//
// NeXT doesn't document what error codes these generate, very well..
//
theCharacter = NXGetc(TheFile);
NXUngetc(TheFile);
[self StoreErrorCode: ERR_OK AndText: "Read the character OK"];
d180 13
a192 5
//
// NeXT doesn't document what error codes these generate, very well..
//
NXUngetc(TheFile);
[self StoreErrorCode: ERR_OK AndText: "Ungot it ok"];
d248 82
@